/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package millenium3dchess;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JComponent;
import millenium3dchess.Game.Color;

/**
 *
 * @author Kenneth
 */
public class GameSquare extends JComponent {

    private Chessman piece;
    final private Gamevector position;
    public boolean highlight = false;
    final Color color;

    public GameSquare(Gamevector pos, Chessman man, Color c) {
        if (man == null) {
            piece = new Defaultman(pos);
        } else {
            piece = man;
        }
        position = pos;
        color = c;
        setSize(new Dimension(80, 80));
        setPreferredSize(new Dimension(80, 80));
    }

    public GameSquare(int x, int y, int z, Chessman man, Color c) {
        position = new Gamevector(x, y, z);
        if (man == null) {
            piece = new Defaultman(position);
        } else {
            piece = man;
        }
        color = c;
    }

    public boolean occupied() {
        return !(piece instanceof Defaultman);
    }

    public void setChessman(Chessman man) {
        if (man == null) {
            piece = new Defaultman(position);
        } else {
            piece = man;
        }
    }

    public Chessman getChessman() {
        return piece;
    }

    public Gamevector getPosition() {
        return position;
    }

    public String getLegalPos() {
        String x = ((Integer) (position.getX() + 1)).toString();
        String y = "_";
        switch (position.getY()) {
            case 0:
                y = "a";
                break;
            case 1:
                y = "b";
                break;
            case 2:
                y = "c";
                break;
            case 3:
                y = "d";
                break;
            case 4:
                y = "e";
                break;
            case 5:
                y = "f";
                break;
            case 6:
                y = "g";
                break;
            case 7:
                y = "h";
                break;
        }
        String z = ((Integer) (position.getZ() + 1)).toString();
        return z + y + x;
    }

    public JComponent preview() {
        return new PreviewGameSquare(position, piece, color);
    }

    @Override
    public void paintComponent(Graphics g) {
        if (color == Color.WHITE) {
            g.setColor(java.awt.Color.CYAN);
        } else {
            g.setColor(java.awt.Color.GREEN);
        }
        g.clearRect(0, 0, getPreferredSize().width, getPreferredSize().height);
        g.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
        if (occupied()) {
            g.drawImage(piece.icon, 0, 0, null);
        }
        g.setColor(java.awt.Color.WHITE);
        g.setFont(new Font("sansserif", Font.BOLD, 12));
        g.drawString(position.toString(), 10, 10);
        if (highlight) {
            g.setColor(new java.awt.Color(1f, 0, 0, .5f));
            g.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
        }
    }

    private class PreviewGameSquare extends GameSquare {

        private PreviewGameSquare(Gamevector pos, Chessman man, Color c) {
            super(pos, man, c);
            setSize(new Dimension(25, 25));
            setPreferredSize(new Dimension(25, 25));
        }

        @Override
        public void paintComponent(Graphics g) {
            if (color == Color.WHITE) {
                g.setColor(java.awt.Color.CYAN);
            } else {
                g.setColor(java.awt.Color.GREEN);
            }
            g.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
            if (occupied()) {
                g.drawImage(piece.smallIcon, 0, 0, null);
            }
        }
    }
}
